package com.tensquare.qa.controller;

import com.alibaba.fastjson.JSON;
import com.tensquare.qa.client.LabelClient;
import com.tensquare.qa.pojo.Label;
import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;
import entity.PageResult;
import entity.Result;
import entity.Result00;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("problem")
public class ProblemController {
    @Autowired
    private ProblemService problemService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LabelClient labelClient;
    //1.查询最新问答列表
    @GetMapping("newlist/{labelId}/{page}/{size}")
    public Result newlist(@PathVariable String labelId,@PathVariable int page,@PathVariable int size){
        PageResult<Problem> pageResult=problemService.newlist(labelId,page,size);
        return new Result(true, StatusCode.OK,"查询最新问答列表成功",pageResult);
    }
    //2.查询热门问答列表
    @GetMapping("hotlist/{labelId}/{page}/{size}")
    public Result hotlist(@PathVariable String labelId,@PathVariable int page,@PathVariable int size){
       PageResult<Problem> pageResult=problemService.hotlist(labelId,page,size);
       return new Result(true, StatusCode.OK,"查询热门问答列表成功",pageResult);
    }
    //3.查询label列表
    @GetMapping("label/list")
    public Result list(){
//        String URL="http://TENSQUARE-BASE";
//        Result00 result = restTemplate.getForObject(URL + "/label", Result00.class);
//        List<Label> labels = JSON.parseArray(result.getData(), Label.class);
//        return new Result(result.isFlag(),result.getCode(),result.getMessage(),labels);
        return labelClient.findAll();
    }
    //4.根据id查询label
    @GetMapping("label/{labelId}")
    public Result findById(@PathVariable String labelId){
        return labelClient.findById(labelId);
    }
}
